home *** CD-ROM | disk | FTP | other *** search
/ Dr. Windows 3 / dr win3.zip / dr win3 / PROGRAMR / OLE2BOOK.ZIP / CHAP02.ZIP / CHAP02 / PATRON / PAGES.CPP < prev    next >
C/C++ Source or Header  |  1993-06-07  |  15KB  |  723 lines

  1. /*
  2.  * PAGES.CPP
  3.  * Original Starter Chapter 2
  4.  *
  5.  * Implementation of the CPages class.  See PAGEWIN.CPP for additional
  6.  * member functions.
  7.  *
  8.  * Copyright (c)1993 Microsoft Corporation, All Rights Reserved
  9.  *
  10.  * Kraig Brockschmidt, Software Design Engineer
  11.  * Microsoft Systems Developer Relations
  12.  *
  13.  * Internet  :  kraigb@microsoft.com
  14.  * Compuserve:  >INTERNET:kraigb@microsoft.com
  15.  */
  16.  
  17.  
  18.  
  19. #include "patron.h"
  20.  
  21.  
  22. HWND        g_hDlgPrint=NULL;
  23. BOOL        g_fCancelPrint=FALSE;
  24.  
  25.  
  26. /*
  27.  * CPages:CPages
  28.  * CPages::~CPages
  29.  *
  30.  * Constructor Parameters:
  31.  *  hInst           HINSTANCE of the application we're in.
  32.  */
  33.  
  34. CPages::CPages(HINSTANCE hInst)
  35.     : CWindow(hInst)
  36.     {
  37.     m_cPages=0;
  38.     m_iPageCur=0xFFFF;  //Pages are 0 indexed, so this is one before that.
  39.     m_hWndPageList=NULL;
  40.  
  41.     /*
  42.      * Initialize to 2.54cm*2.54cm which is a page with no space for anything,
  43.      * just margins.  2.54cm=.5 inches on each margin.
  44.      */
  45.     m_cx=LOMETRIC_PER_INCH;
  46.     m_cy=LOMETRIC_PER_INCH;
  47.  
  48.     m_xPos=0L;
  49.     m_yPos=0L;
  50.  
  51.     m_dwIDNext=0;
  52.  
  53.     m_hDevMode=NULL;
  54.     return;
  55.     }
  56.  
  57.  
  58. CPages::~CPages(void)
  59.     {
  60.     //Insure memory is cleaned up in the list
  61.     New();
  62.  
  63.     if (NULL!=m_hFont && !m_fSystemFont)
  64.         DeleteObject(m_hFont);
  65.  
  66.     if (NULL!=m_hWndPageList)
  67.         DestroyWindow(m_hWndPageList);
  68.  
  69.     return;
  70.     }
  71.  
  72.  
  73.  
  74.  
  75.  
  76. /*
  77.  * CPages::FInit
  78.  *
  79.  * Purpose:
  80.  *  Instantiates a pages window within a given parent.  The
  81.  *  parent may be a main application window, could be an MDI child
  82.  *  window. We really do not care.
  83.  *
  84.  * Parameters:
  85.  *  hWndParent      HWND of the parent of this window
  86.  *  pRect           LPRECT that this window should occupy
  87.  *  dwStyle         DWORD containing the window's style flags.  Should
  88.  *                  contain WS_CHILD | WS_VISIBLE in typical circumstances.
  89.  *  uID             UINT ID to associate with this window
  90.  *  pv              LPVOID unused for now.
  91.  *
  92.  * Return Value:
  93.  *  BOOL            TRUE if the function succeeded, FALSE otherwise.
  94.  */
  95.  
  96. BOOL CPages::FInit(HWND hWndParent, LPRECT pRect, DWORD dwStyle
  97.     , UINT uID, LPVOID pv)
  98.     {
  99.     int     cy;
  100.  
  101.     m_hWnd=CreateWindowEx(WS_EX_NOPARENTNOTIFY, SZCLASSPAGES
  102.         , SZCLASSPAGES, dwStyle, pRect->left, pRect->top
  103.         , pRect->right-pRect->left, pRect->bottom-pRect->top
  104.         , hWndParent, (HMENU)uID, m_hInst, (LPVOID)this);
  105.  
  106.     if (NULL==m_hWnd)
  107.         return FALSE;
  108.  
  109.     /*
  110.      * Create the hidden listbox we'll use to track pages.  We give it
  111.      * the owner-draw style so we can just store pointers in it.
  112.      */
  113.     m_hWndPageList=CreateWindow("listbox", "Page List", WS_POPUP | LBS_OWNERDRAWFIXED
  114.         , 0, 0, 100, 100, HWND_DESKTOP, NULL, m_hInst, NULL);
  115.  
  116.     if (NULL==m_hWndPageList)
  117.         return FALSE;
  118.  
  119.     //Create a 14 point Arial font, or use the system variable font.
  120.     cy=MulDiv(-14, LOMETRIC_PER_INCH, 72);
  121.     m_hFont=CreateFont(cy, 0, 0, 0, FW_NORMAL, FALSE, FALSE, FALSE
  122.         , ANSI_CHARSET, OUT_TT_PRECIS, CLIP_TT_ALWAYS, PROOF_QUALITY
  123.         , VARIABLE_PITCH | FF_SWISS, "Arial");
  124.  
  125.     if (NULL==m_hFont)
  126.         {
  127.         m_hFont=(HFONT)GetStockObject(ANSI_VAR_FONT);
  128.         m_fSystemFont=TRUE;
  129.         }
  130.  
  131.     return TRUE;
  132.     }
  133.  
  134.  
  135.  
  136.  
  137.  
  138.  
  139.  
  140. /*
  141.  * CPages::New
  142.  *
  143.  * Purpose:
  144.  *  Cleans out and reinitializes the data to defaults.
  145.  *
  146.  * Parameters:
  147.  *  None
  148.  *
  149.  * Return Value:
  150.  *  None
  151.  */
  152.  
  153. void CPages::New(void)
  154.     {
  155.     LPPAGE      pPage;
  156.     UINT        i;
  157.  
  158.     //First walk the page list and clean out the allocations.
  159.     for (i=0; i < m_cPages; i++)
  160.         {
  161.         if (FPageGet(i, &pPage, FALSE))
  162.             delete pPage;
  163.         }
  164.  
  165.     if (NULL!=m_hWndPageList)
  166.         SendMessage(m_hWndPageList, LB_RESETCONTENT, 0, 0L);
  167.  
  168.     if (NULL!=m_hDevMode)
  169.         GlobalFree(m_hDevMode);
  170.  
  171.     return;
  172.     }
  173.  
  174.  
  175.  
  176.  
  177.  
  178.  
  179.  
  180.  
  181. /*
  182.  * CPages::RectGet
  183.  *
  184.  * Purpose:
  185.  *  Returns the rectangle of the Pages window in parent coordinates.
  186.  *
  187.  * Parameters:
  188.  *  pRect           LPRECT in which to return the rectangle.
  189.  *
  190.  * Return Value:
  191.  *  None
  192.  */
  193.  
  194. void CPages::RectGet(LPRECT pRect)
  195.     {
  196.     RECT        rc;
  197.     POINT       pt;
  198.  
  199.     //Retrieve the size of our rectangle in parent coordinates.
  200.     GetWindowRect(m_hWnd, &rc);
  201.     pt.x=rc.left;
  202.     pt.y=rc.top;
  203.     ScreenToClient(GetParent(m_hWnd), &pt);
  204.  
  205.     SetRect(pRect, pt.x, pt.y, pt.x+(rc.right-rc.left)
  206.         , pt.y+(rc.bottom-rc.top));
  207.  
  208.     return;
  209.     }
  210.  
  211.  
  212.  
  213.  
  214.  
  215.  
  216. /*
  217.  * CPages::RectSet
  218.  *
  219.  * Purpose:
  220.  *  Sets a new rectangle for the Pages window which sizes to fit.
  221.  *  Coordinates are given in parent terms.
  222.  *
  223.  * Parameters:
  224.  *  pRect           LPRECT containing the new rectangle.
  225.  *  fNotify         BOOL indicating if we're to notify anyone of the change.
  226.  *
  227.  * Return Value:
  228.  *  None
  229.  */
  230.  
  231. void CPages::RectSet(LPRECT pRect, BOOL fNotify)
  232.     {
  233.     UINT        cx, cy;
  234.  
  235.     if (NULL==pRect)
  236.         return;
  237.  
  238.     cx=pRect->right-pRect->left;
  239.     cy=pRect->bottom-pRect->top;
  240.  
  241.     SetWindowPos(m_hWnd, NULL, pRect->left, pRect->top
  242.         , (UINT)cx, (UINT)cy, SWP_NOZORDER);
  243.  
  244.     UpdateScrollRanges();
  245.     return;
  246.     }
  247.  
  248.  
  249.  
  250.  
  251. /*
  252.  * CPages::SizeGet
  253.  *
  254.  * Purpose:
  255.  *  Retrieves the size of the pages window in parent coordinates.
  256.  *
  257.  * Parameters:
  258.  *  pRect           LPRECT in which to return the size.  The right and
  259.  *                  bottom fields will contain the dimensions.
  260.  *
  261.  * Return Value:
  262.  *  None
  263.  */
  264.  
  265. void CPages::SizeGet(LPRECT pRect)
  266.     {
  267.     RectGet(pRect);
  268.     return;
  269.     }
  270.  
  271.  
  272.  
  273.  
  274.  
  275.  
  276.  
  277. /*
  278.  * CPages::SizeSet
  279.  *
  280.  * Purpose:
  281.  *  Sets a new size in parent coordinates for the Pages window.
  282.  *
  283.  * Parameters:
  284.  *  pRect           LPRECT containing the new rectangle.
  285.  *  fNotify         BOOL indicating if we're to notify anyone of the change.
  286.  *
  287.  * Return Value:
  288.  *  None
  289.  */
  290.  
  291. void CPages::SizeSet(LPRECT pRect, BOOL fNotify)
  292.     {
  293.     UINT        cx, cy;
  294.  
  295.     if (NULL==pRect)
  296.         return;
  297.  
  298.     cx=pRect->right-pRect->left;
  299.     cy=pRect->bottom-pRect->top;
  300.  
  301.     SetWindowPos(m_hWnd, NULL, 0, 0, (UINT)cx, (UINT)cy
  302.         , SWP_NOMOVE | SWP_NOZORDER);
  303.  
  304.     UpdateScrollRanges();
  305.     return;
  306.     }
  307.  
  308.  
  309.  
  310.  
  311.  
  312.  
  313. /*
  314.  * CPages::PageInsert
  315.  *
  316.  * Purpose:
  317.  *  Creates a new page immediately after the current page.  If there
  318.  *  are no pages then this creates page 1.
  319.  *
  320.  * Parameters:
  321.  *  uReserved       UINT unused
  322.  *
  323.  * Return Value:
  324.  *  UINT            Index of the new page, 0 on failure.
  325.  */
  326.  
  327. UINT CPages::PageInsert(UINT uReserved)
  328.     {
  329.     //Create and open the new page.
  330.     if (!FPageAdd(m_iPageCur, m_dwIDNext, TRUE))
  331.         return 0;
  332.  
  333.     m_dwIDNext++;
  334.     m_iPageCur++;
  335.     m_cPages++;
  336.  
  337.     InvalidateRect(m_hWnd, NULL, FALSE);
  338.     UpdateWindow(m_hWnd);
  339.     return m_iPageCur;
  340.     }
  341.  
  342.  
  343.  
  344.  
  345.  
  346.  
  347.  
  348. /*
  349.  * CPages::PageDelete
  350.  *
  351.  * Removes the current page from the page list.
  352.  *
  353.  * Parameters:
  354.  *  uReserved       UINT unused
  355.  *
  356.  * Return Value:
  357.  *  UINT            Index to the now current page from the page list,
  358.  *                  -1 on error.
  359.  */
  360.  
  361. UINT CPages::PageDelete(UINT uReserved)
  362.     {
  363.     LPPAGE      pPage;
  364.  
  365.     if (!FPageGet(m_iPageCur, &pPage, FALSE))
  366.         return -1;
  367.  
  368.     //Delete the page in both the the listbox and in memory.
  369.     SendMessage(m_hWndPageList, LB_DELETESTRING, m_iPageCur, 0L);
  370.  
  371.     delete pPage;
  372.  
  373.     /*
  374.      * If this is the last page then the current is one less.  If it's
  375.      * the only page the current is zero.  Otherwise the current is the
  376.      * next page.
  377.      */
  378.  
  379.     if (m_iPageCur==m_cPages-1)   //Covers last or only page.
  380.         m_iPageCur--;
  381.  
  382.     m_cPages--;
  383.  
  384.     if (0!=m_cPages)
  385.         InvalidateRect(m_hWnd, NULL, FALSE);
  386.     else
  387.         InvalidateRect(m_hWnd, NULL, TRUE);
  388.  
  389.     UpdateWindow(m_hWnd);
  390.     return m_iPageCur;
  391.     }
  392.  
  393.  
  394.  
  395.  
  396.  
  397.  
  398. /*
  399.  * CPages::CurPageGet
  400.  *
  401.  * Purpose:
  402.  *  Retrieves the index of the current page we're viewing.
  403.  *
  404.  * Parameters:
  405.  *  None
  406.  *
  407.  * Return Value:
  408.  *  UINT